//
//  SearchTableViewController.m
//  HandheldHospital
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "SearchTableViewController.h"
#import "SearchTableViewCell.h"

@interface SearchTableViewController ()<UISearchBarDelegate>
//数据源
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *filterArray;
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic,strong) NSMutableArray *SearchArray;

@end

@implementation SearchTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTableViews];
}
//TableView设置
- (void)addTableViews
{
    //隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    //设置tableView大小
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20,[UIScreen  mainScreen].bounds.size.width ,[UIScreen  mainScreen].bounds.size.height)];
    self.searchBar  = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.tableView.frame.size.width, 44)];
    [self.tableView addSubview:self.searchBar];
    self.searchBar.delegate = self;
    //注册Cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SearchTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SearchCell"];
    
    self.SearchArray = [NSMutableArray arrayWithArray:@[@"医学科学院北京协和医院",@"中国医学科学院肿瘤医院",@"医科大附属北京儿童医院",@"首都医科大学原宣武医院",@"医科大附属北京朝阳医院",@"中日友好医院",@"北京老年医院",@"北京军区医院",@"北京京煤集团总医院",@"中国解放军总总医院",@"北京医院",@"北京妇产科医院",@"北京同仁医院",@"山西阳光男科医院",@"济南蓝翔肛肠医院",@"北京世纪坛医院",@"辽宁天堂医院",@"山西儿童医院",@"山西医科大学第一医院",@"上海长海医院",@"第三人民医院",@"京城皮肤病医院",@"北京中医医院",@"上海中山医院",@"复旦大学附属肿瘤医院",@"季世堂医院"]];
    self.tableView.tableHeaderView = self.searchBar;
    
    //设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//懒加载
-(NSMutableArray *)SearchArray{
    if (_SearchArray == nil) {
        _SearchArray = [NSMutableArray array];
    }
    return _SearchArray
    ;
}

- (NSMutableArray *)dataArray
{
    if(_dataArray == nil)
    {
        _dataArray = [NSMutableArray arrayWithArray:self.SearchArray];
    }
    return _dataArray;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
    
    cell.SearchLabei.text = self.dataArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSString *str = searchBar.text;
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", str];
    if (self.filterArray!= nil) {
        [self.filterArray removeAllObjects];
    }
    //过滤数据
    self.filterArray= [NSMutableArray arrayWithArray:[self.SearchArray filteredArrayUsingPredicate:preicate]];
    if(self.dataArray != nil)
    {
        [self.dataArray removeAllObjects];
    }
    self.dataArray = self.filterArray;
    //刷新表格
    [self.tableView reloadData];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
